package com.tcwlkj.uuService.widget.chatview;

import android.content.Context;
import android.content.res.Configuration;
import android.graphics.Canvas;
import android.graphics.drawable.BitmapDrawable;
import android.util.AttributeSet;
import android.widget.RelativeLayout;

import com.tcwlkj.uuService.R;


/**
 * Created by ${tanbo} on 2017/8/10 0010.
 */

public class ResizeLayout extends RelativeLayout {

    public static final byte KEYBOARD_STATE_SHOW = -3; // 软键盘显示状态
    public static final byte KEYBOARD_STATE_HIDE = -2; // 软键盘隐藏状态
    public static final byte KEYBOARD_STATE_INIT = -1; // 软键盘初始化状态
    private int filterHeight;// 过滤高度
    private boolean mHasInit; // 初始标记
    private boolean mHasKeybord;// 是否被挤压到一定程度标记
    private int mHeight; // 底部距离
    private OnKybdsChangeListener mListener; // 监听接口
    private int mState;
    /**
     * 控件的最大高度
     */
    private int maxHeight;

    /**
     * 控件的对打宽度
     */
    private int maxWidth;

    public ResizeLayout(Context context) {
        super(context);
    }

    public ResizeLayout(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    public ResizeLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
        filterHeight = getResources().getDimensionPixelSize(R.dimen.main_footer_height);
    }

    /**
     * 键盘监听，实际上是以布局被压缩的原来达到检查是否有软键盘弹出的效果
     *
     * @param listener
     */
    public void setOnkbdStateListener(OnKybdsChangeListener listener) {
        mListener = listener;
    }

    /**
     * 布局被挤压事件
     */
    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        super.onLayout(changed, l, t, r, b);
        if(getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT){
            maxWidth = Math.max(maxWidth, r - l);
            maxHeight = Math.max(maxHeight, b - t);
        }
        // added by ex-yangzhihong001
        if (!mHasInit) {
            mHasInit = true;
            mHeight = b;
            if (mListener != null && mState != KEYBOARD_STATE_INIT) {
                mState = KEYBOARD_STATE_INIT;
                mListener.onKeyBoardStateChange(KEYBOARD_STATE_INIT);
            }
        } else {
            mHeight = mHeight < b ? b : mHeight;
        }
        if (mHasInit && mHeight > b + filterHeight) {
            mHasKeybord = true;
            if (mListener != null && mState != KEYBOARD_STATE_SHOW) {
                mState = KEYBOARD_STATE_SHOW;
                mListener.onKeyBoardStateChange(KEYBOARD_STATE_SHOW);
            }
        }
        if (mHasInit && mHasKeybord && mHeight == b) {
            mHasKeybord = false;
            if (mListener != null && mState != KEYBOARD_STATE_HIDE) {
                mState = KEYBOARD_STATE_HIDE;
                mListener.onKeyBoardStateChange(KEYBOARD_STATE_HIDE);
            }
        }
    }

    /** 监听软键盘状态回调接口 */
    public static interface OnKybdsChangeListener {
        public void onKeyBoardStateChange(int state);
    }

    public void setFilterHeight(int bottomHeight) {
        this.filterHeight = bottomHeight;
    }

    // added by ex-yangzhihong001
    // 背景图片
    private BitmapDrawable backgroudDrawable;

    @Override
    protected void dispatchDraw(Canvas canvas) {
        // 画背景
        if (backgroudDrawable != null) {
            backgroudDrawable.setBounds(0, 0, maxWidth, maxHeight);
            backgroudDrawable.draw(canvas);
        }
        super.dispatchDraw(canvas);
    }

    /**
     * 设置不被压缩的背景
     */
    public void setBackground2(BitmapDrawable drawable) {
        if (backgroudDrawable != null && backgroudDrawable.getBitmap() != null) {
            backgroudDrawable.getBitmap().recycle();
        }
        backgroudDrawable = drawable;
        invalidate();
    }
}

